<?php
/**
 * Created by PhpStorm.
 * User: HS032
 * Date: 2020/9/14
 * Time: 19:18
 */

namespace ticket\controllers;


use ticket\modules\backend\models\CommonApp;
use yii\log\FileTarget;
use yii\web\Controller;
use Yii;

class BasicController extends Controller
{

    public function actionOpenApp()
    {
        if (\Yii::$app->request->isPost){
            $post = \Yii::$app->request->post();
            $appCommentModel = new CommonApp();
            $this->recordLogin(10001,'开通APP实例成功',json_encode($post,JSON_UNESCAPED_UNICODE));

            if(($appInfo = $appCommentModel->addApp($post)) !== false){
                $backInfo = [0, '开通APP实例成功'];//返回信息
                //开通直播间
//                Live::add(time(),$post['app_name'],$appInfo['appid']);
                $this->recordLogin(10001,'开通APP实例成功',json_encode($appInfo));
                return $this->showRes($backInfo,['appinfo' => $appInfo]);
            }
        }
        return $this->showRes([0, '开通APP实例失败']);

        //http://paycentre.horsevision.cn/index.php?r=

    }


    protected function setSysLog($content)
    {
        $userInfo = Yii::$app->session->get('loginInfo')->userInfo;
        $patams = [
            'SysLog' => [
                'user_id' => isset($userInfo['admin_id'])?$userInfo['admin_id']:1,
                'username' => isset($userInfo['username'])?$userInfo['username']:'admin',
                'login_add' => '',
                'login_ip' => Yii::$app->request->userIP,
                'content' => $content,
                'operate_time' => date("Y-m-d H:i:s"),
            ]
        ];
//        var_dump($patams);die;
        $sysModel = new Syslog();
        if ($sysModel->addInfo($patams)){
            return true;
        }else{
            return false;
        }
    }



    protected function getAppId($appId)
    {
        $appModel = new CommonApp();
        $app_id = $appModel->getAppId($appId);
        $this->appId = $app_id;
        return true;
    }



    /*
    返回客户端函数
    $args = array(
        '0'  返回码
        '1'  返回信息
    )
    $data  返回数据，一般为数组，比如获取信息列表信息的时候
    $url   返回链接，跳转链接，比如微信登录的时候
    */
    public static function showRes($args = [0, '操作成功'], $data = '', $url = '')
    {
        $res = array(
            'code' => $args[0],
            'info' => $args[1]
        );
        if(!empty($data)){
            $res['data'] = $data;
        }

        if(!empty($url)){
            $res['url'] = $url;
        }
        return json_encode($res, JSON_UNESCAPED_UNICODE);
//        return $res;//JSON_UNESCAPED_UNICODE + JSON_UNESCAPED_SLASHES = 320  中文/ 同时生效
    }


    protected function recordLogin($code,$msg,$data)
    {
        $log = new FileTarget();
        $log->logFile = Yii::$app->getRuntimePath() . '/logs/send_' . date('Ymd') . '.log';
        $log->messages[] = [
            "[url:{$_SERVER['REQUEST_URI']}],[data:{$code},{$msg},{$data}]",
            1,
            'application',
            time(),
        ];
        $log->export();
    }
}